package cn.iocoder.yudao.module.wine.dal.mysql.stockstore;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.wine.controller.app.stockstore.vo.AppStockStoreListReqVO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.stockstore.StockStoreDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wine.controller.admin.stockstore.vo.*;

/**
 * 仓库信息 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface StockStoreMapper extends BaseMapperX<StockStoreDO> {

    default PageResult<StockStoreRespVO> selectPage(StockStorePageReqVO reqVO) {
        return selectJoinPage(reqVO, StockStoreRespVO.class, new MPJLambdaWrapperX<StockStoreDO>()
                .eqIfPresent(StockStoreDO::getUserId, reqVO.getUserId())
                .eqIfPresent(StockStoreDO::getAreaId, reqVO.getAreaId())
                .likeIfPresent(StockStoreDO::getName, reqVO.getName())
                .eqIfPresent(StockStoreDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(StockStoreDO::getCreateTime, reqVO.getCreateTime())
                .leftJoin(MemberUserDO.class,MemberUserDO::getId,StockStoreDO::getUserId)
                .selectAll(StockStoreDO.class)
                .selectAs(MemberUserDO::getName, StockStoreRespVO::getRealName)
                .orderByDesc(StockStoreDO::getId));
    }

    default List<StockStoreDO> getStockStoreList(AppStockStoreListReqVO reqVO) {
        //地区
        List<Integer> areaList = new ArrayList<>();
        if (reqVO.getAreaId() != null){
            Integer areaId = reqVO.getAreaId();
            Area area = AreaUtils.getArea(areaId);
            if(area.getType().equals(AreaTypeEnum.PROVINCE.getType())){
                List<Area> children = area.getChildren();
                List<Integer> collect = children.stream().map(item -> item.getId()).collect(Collectors.toList());
                collect.forEach(item -> {
                    Area area1 = AreaUtils.getArea(item);
                    List<Integer> collect1 = area1.getChildren().stream().map(data -> data.getId()).collect(Collectors.toList());
                    areaList.addAll(collect1);
                });
                areaList.addAll(collect);
            }
            if (area.getType().equals(AreaTypeEnum.CITY.getType())){
                List<Area> children = area.getChildren();
                List<Integer> collect = children.stream().map(item -> item.getId()).collect(Collectors.toList());
                areaList.addAll(collect);
            }
        }
        //关键词
        String keywords = reqVO.getKeywords();
        if (!StringUtils.isEmpty(keywords)) {
            String[] split = keywords.split(" ");
            ArrayList<StockStoreDO> list = new ArrayList<>();
            for (String s : split) {
                List<StockStoreDO> stockStoreDOS = selectList(new LambdaQueryWrapperX<StockStoreDO>()
                        .eqIfPresent(StockStoreDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                        .eqIfPresent(StockStoreDO::getStoreType, reqVO.getStoreType())
                        .likeIfPresent(StockStoreDO::getName, s)
                        .likeIfPresent(StockStoreDO::getRemark, s)
                        .in(!CollectionUtil.isEmpty(areaList), StockStoreDO::getAreaId,areaList)
                        .orderByDesc(StockStoreDO::getId));
                list.addAll(stockStoreDOS);
            }
            if (CollectionUtil.isEmpty(list)) {
                return list.stream().distinct().collect(Collectors.toList());
            }
            return list;
        }

        return selectList(new LambdaQueryWrapperX<StockStoreDO>()
                .eqIfPresent(StockStoreDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                .eqIfPresent(StockStoreDO::getStoreType, reqVO.getStoreType())
                .likeIfPresent(StockStoreDO::getName, reqVO.getName())
                .likeIfPresent(StockStoreDO::getRemark, reqVO.getRemark())
                .in(!CollectionUtil.isEmpty(areaList), StockStoreDO::getAreaId,areaList)
                .orderByDesc(StockStoreDO::getId));
    }
}
